package page.sample.AdministratorPage;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import page.MyFirstWebPage;
import page.sample.AuthorizationPage;
import page.sample.ClientPage.ShowCreditCardPage;
import dao.impl.ClientDao;
import entity.Client;
import entity.CreditCard;
import entity.InfoUser;
import entity.Operations;

public class UnlockCardPage extends WebPage {

	public UnlockCardPage(final Client client, final InfoUser infoUser) {
		
		final Operations op = new Operations();
		final ClientDao clientDao = new ClientDao(app.MyFirstWebApplication.DATA_XML_FOLDER);
		
		for(Client cl:clientDao.getAll())
		{
			if(cl.getInfoUser().getLogin().equals(client.getInfoUser().getLogin()))
			{
				client.setCreditCards(cl.getCreditCards());
			}
		}
		
		final List<CreditCard> allCreditCards = client.getCreditCards();

		add(new DataView<CreditCard>("table-rows", new ListDataProvider<CreditCard>(
				allCreditCards)) {
			@Override
			protected void populateItem(final Item<CreditCard> item) {
				final CreditCard creditCard = item.getModelObject();
				// add data for table cell in row
				item.add(new Label("account", creditCard.getAccount()));
				item.add(new Label("variableLock", creditCard.isVariableLock()));
				
				item.add(new Link("unlock") {
					@Override
					public void onClick() {
						op.unlock(creditCard.getAccount(), client);
						clientDao.update(client);
						setResponsePage(new UnlockCardPage(client, infoUser));
					}
				});
			}
		});
		
		add(new Link("ListClients")
		{

			@Override
			public void onClick() {
				setResponsePage(new ListClientsPage(infoUser));
				
			}
			
		});
		
		add(new Link("exit")
		{

			@Override
			public void onClick() {
				setResponsePage(new AuthorizationPage());
				
			}
			
		});
	}

}
